import Vue from 'vue'
import VueRouter from 'vue-router'
import LoginView from '../views/LoginView.vue'
import RegView from "@/views/RegView";
import el from "element-ui/src/locale/lang/el";
import axios from "axios";

Vue.use(VueRouter)

const routes = [

  {
    path: '/home',
    component: () => import('../views/HomeView.vue'),
    children: [
      {
        path:"/home",
        component: () => import('../views/home/HomeIndex.vue'),
        meta:{
          title: "首页"
        }
      },
      {
        path:"/home/item/info",
        component: () => import('../views/home/HomeItemInfo.vue'),
        meta:{
          title: "物资信息"
        }
      },
      {
        path:"/home/item/apply",
        component: () => import('../views/home/HomeItemApply.vue'),
        meta:{
          title: "物资申请"
        }
      },
      {
        path:"/home/item/donation",
        component: () => import('../views/home/HomeItemDonation.vue'),
        meta:{
          title: "物资捐献"
        }
      },
      {
        path:"/home/bbs",
        component: () => import('../views/home/HomeBBS.vue'),
        meta:{
          title: "疫情论坛"
        }
      },
      {
        path:"/home/news",
        component: () => import('../views/home/HomeNews.vue'),
        meta:{
          title: "疫情资讯"
        }
      },
      {
        path:"/home/message",
        component: () => import('../views/home/HomeMessage.vue'),
        meta:{
          title: "留言反馈"
        }
      },
      {
        path:"/home/user/info",
        component: () => import('../views/home/HomeUserInfo.vue'),
        meta:{
          title: "个人中心"
        }
      },
      {
        path:"/home/management",
        component: () => import('../views/home/HomeManagement.vue'),
        meta:{
          title: "后台管理"
        }
      },
      {
        path:"/home/newsDetail",
        component: () => import('../views/home/NewsDetail.vue'),
        meta:{
          title: "后台管理"
        }
      },
      {
        path:"/home/ItemDetail",
        component: () => import('../views/home/ItemDetail.vue'),
        meta:{
          title: "后台管理"
        }
      },
    ]
  },
  {
    path: '/login',
    component: LoginView

  },
  {
    path: '/reg',
    component: RegView

  },
  {
    path: '/user',
    component: () => import('../views/UserView.vue'),
    children: [
      {
        path:"/user",
        component: () => import('../views/user/UserInfo.vue'),
        meta:{
          title: "个人中心"
        }
      },
      {
        path:"/user/manage/item/info",
        component: () => import('../views/user/UserManageItemInfo.vue'),
        meta:{
          title: "物资信息管理"
        }
      },
      {
        path:"/user/manage/item/apply",
        component: () => import('../views/user/UserManageItemApply.vue'),
        meta:{
          title: "物资申请管理"
        }
      },
      {
        path:"/user/manage/item/donation",
        component: () => import('../views/user/UserManageItemDonation.vue'),
        meta:{
          title: "物资捐献管理"
        }
      },
    ]
  },
  {
    path: '/admin',
    component: () => import('../views/AdminView.vue'),
    children: [
      {
        path:"/admin",
        component: () => import('../views/admin/AdminInfo.vue'),
        meta:{
          title: "个人中心"
        }
      },
      {
        path:"/admin/manage/user",
        component: () => import('../views/admin/AdminManageUser.vue'),
        meta:{title: "用户管理"}
      },
      {
        path:"/admin/manage/emp",
        component: () => import('../views/admin/AdminManageEmp.vue'),
        meta:{title: "员工管理"},
      },
      {
        path:"/admin/manage/item/info",
        component: () => import('../views/admin/AdminManageItemInfo.vue'),
        meta:{
          title: "物资信息管理"
        }
      },
      {
        path:"/admin/manage/item/apply",
        component: () => import('../views/admin/AdminManageItemApply.vue'),
        meta:{
          title: "物资申请管理"
        }
      },
      {
        path:"/admin/manage/item/donation",
        component: () => import('../views/admin/AdminManageItemDonation.vue'),
        meta:{
          title: "物资捐献管理"
        }
      },
      {
        path:"/admin/manage/item/in",
        component: () => import('../views/admin/AdminManageItemIn.vue'),
        meta:{
          title: "物资入库管理"
        }
      },
      {
        path:"/admin/manage/item/out",
        component: () => import('../views/admin/AdminManageItemOut.vue'),
        meta:{
          title: "物资发放记录"
        }
      },
      {
        path:"/admin/manage/message",
        component: () => import('../views/admin/AdminManageMessage.vue'),
        meta:{
          title: "留言板管理"
        }
      },
      {
        path:"/admin/manage/bbs",
        component: () => import('../views/admin/AdminManageBbs.vue'),
        meta:{
          title: "论坛管理"
        }
      },
      {
        path:"/admin/manage/news",
        component: () => import('../views/admin/AdminManageNews.vue'),
        meta:{
          title: "系统管理"
        }
      },
      {
        path:"/admin/manage/show",
        component: () => import('../views/admin/AdminManageShow.vue'),
        meta:{
          title: "系统管理"
        }
      },
    ]
  },
  //  管理员界面-----用户管理
  {
    path:"/admin/manage/user/update/details",
    component: () => import('../views/admin/user/UserUpdateDetails.vue'),
    meta:{title: "员工信息修改"},
  },
  {
    path:"/admin/manage/user/details",
    component: () => import('../views/admin/user/UserDetails.vue'),
    meta:{title: "员工信息查看"},
  },
  {
    path:"/admin/manage/user/add",
    component: () => import('../views/admin/user/UserAdd.vue'),
    meta:{title: "增加员工"},
  },
  //  管理员界面-----员工管理
  {
    path:"/admin/manage/emp/update/details",
    component: () => import('../views/admin/emp/EmpUpdateDetails.vue'),
    meta:{title: "员工信息修改"},
  },
  {
    path:"/admin/manage/emp/details",
    component: () => import('../views/admin/emp/EmpDetails.vue'),
    meta:{title: "员工信息查看"},
  },
  {
    path:"/admin/manage/emp/add",
    component: () => import('../views/admin/emp/EmpAdd.vue'),
    meta:{title: "增加员工"},
  },
    // 资讯管理
  {
    path:"/admin/manage/informationAdd",
    component: () => import('../views/admin/information/InformationAdd.vue'),
    meta:{title: "增加资讯"},
  },
  {
    path:"/admin/manage/informationShow",
    component: () => import('../views/admin/information/InformationShow.vue'),
    meta:{title: "查看详情"},
  },
  {
    path:"/admin/manage/informationUpdate",
    component: () => import('../views/admin/information/InformationUpdate.vue'),
    meta:{title: "修改咨询"},
  },
    //轮播图管理
  {
    path:"/admin/manage/ShowAdd",
    component: () => import('../views/admin/show/ShowAdd'),
    meta:{title: "轮播图管理"},
  },

  //  管理员界面-----物资信息管理
  {
    path:"/admin/manage/item/details",
    component: () => import('../views/admin/item/ItemDetails.vue'),
    meta:{title: "查看物资信息"},
  },
  {
    path:"/admin/manage/item/update/details",
    component: () => import('../views/admin/item/ItemUpdateDetails.vue'),
    meta:{title: "查看物资信息"},
  },
  {
    path:"/admin/manage/item/add",
    component: () => import('../views/admin/item/ItemAdd.vue'),
    meta:{title: "添加物资"},
  },
  {
    path:"/admin/manage/item/applyItem",
    component: () => import('../views/admin/item/ItemApply.vue'),
    meta:{title: "申请物资"},
  },
  {
    path:"/error",
    component: () => import('../views/ErrorView.vue'),
    meta:{title: "身份信息异常"},
  },
  {
    path:"*",
    component: () => import('../views/404.vue'),
    meta:{title: "404"},
  },

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to,from,next) => {
  if (to.path.startsWith("/login") || to.path.startsWith("/reg")){
    window.localStorage.removeItem('JWT_TOKEN')
    next()
  }else {
    let jwt = localStorage.getItem('JWT_TOKEN')
    if (jwt === null || jwt ===""){
      next({path:'/login'})
    }else {
      // 后端解析jwt
      // 发请求获取员工信息
      // http://176.17.10.223:18080/user/parse/jwt?jwt=xxxxxxxx
      let url = 'http://176.17.10.223:18080/user/parse/jwt'
      let data = 'jwt='+ window.localStorage.getItem('JWT_TOKEN')
      console.log("请求数据为:" + data)
      //debugger
      axios.post(url,data).then( (response)=> {
        // 如果response报错,需要给出error页面
        console.log(response)
        console.log("status=======" + response.status)
        // 将得到的员工数据存储起来
        localStorage.setItem('user', JSON.stringify(response.data.data));
      }).catch(reason => {
        console.log("校验失败")
        next({path:'/error'})
      })
      next()
    }
  }
})

export default router
